//
//  AdvisoryDetailInfoViewController.m
//  homeland
//
//  Created by SunShine on 13-10-25.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "AdvisoryDetailInfoViewController.h"
#import "AdvisoryDetailInfoCell.h"
#import "AdvisoryBasicViewController.h"

@interface AdvisoryDetailInfoViewController ()
@property (strong, nonatomic) AdvisoryService *dataService;
@property (assign, nonatomic) BOOL onFeedBack;
@end

@implementation AdvisoryDetailInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.fwpjView.hidden = YES;//隐藏服务评价
    // Do any additional setup after loading the view from its nib.
    self.title = @"受理结果";
    NSString *Id = [_config objectForKey:@"id"];
    
    self.navigationItem.rightBarButtonItem = getCustomNavButton(@"基本信息", self, @selector(rightButtonAction:));
    self.onFeedBack = NO;
    self.dataService = [AdvisoryService new];
    _dataService.delegate = self;
    [_dataService queryDetailAdvisoryWithID:Id];
    [self startLoadingView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)rightButtonAction:(id)sender
{
    if (_dataService.detailItem) {
        AdvisoryBasicViewController *basicVC = [[AdvisoryBasicViewController alloc] initWithNibName:@"AdvisoryBasicViewController" bundle:nil];
        basicVC.dataItem = _dataService.detailItem;
        [self.navigationController pushViewController:basicVC animated:YES];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"获取数据失败，请返回上级页面重试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
    if (_dataService.detailItem.voteGrade && _dataService.detailItem.voteGrade != (id)[NSNull null]) {
        //已经投过票了
        _button1.enabled = NO;
        _button2.enabled = NO;
        _button3.enabled = NO;
        NSInteger vote = _dataService.detailItem.voteGrade.integerValue;
        if (vote == 1) {
            _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        } else if (vote == 2) {
            _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        } else if (vote == 3) {
            _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        }
    }
    if (self.onFeedBack) {
        _button1.enabled = NO;
        _button2.enabled = NO;
        _button3.enabled = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"反馈成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Value Buttons Action
- (IBAction)button1Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService feedbackAdvisoryReplyWithID:[_config objectForKey:@"id"] AndVote:1];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button2Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService feedbackAdvisoryReplyWithID:[_config objectForKey:@"id"] AndVote:2];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button3Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    [_dataService feedbackAdvisoryReplyWithID:[_config objectForKey:@"id"] AndVote:3];
    [self startLoadingViewWithText:@"正在提交..."];
}

#pragma mark - Table Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        if (_dataService.detailItem.unitName && _dataService.detailItem.unitName != (id)[NSNull null] && _dataService.detailItem.unitName.length > 0) {
            CGFloat height = [self caculateHeightWithoutPaddingForString:_dataService.detailItem.unitName AndFontSize:14.0 AndWitdh:190.0];
            
            return MAX(51.0, height+26.0);
        }
    }
    if (indexPath.row == 4) {
        if (_dataService.detailItem.replyContent && _dataService.detailItem.replyContent != (id)[NSNull null] && _dataService.detailItem.replyContent.length > 0){
            CGFloat height = [self caculateHeightWithoutPaddingForString:_dataService.detailItem.replyContent AndFontSize:14.0 AndWitdh:170.0];
            
            return MAX(51.0, height+36.0);
        }
    }
    return 51.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"AdvisoryDetailInfoCell";
    AdvisoryDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryDetailInfoCell" owner:self options:nil] objectAtIndex:0];
    }
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"咨询时间";
        cell.detailTextView.text = [_dataService.detailItem.insertTime substringToIndex:10];
    } else if (indexPath.row == 1) {
        cell.titleLabel.text = @"回复状态";
        if (_dataService.detailItem.replyStatus && _dataService.detailItem.replyStatus != (id)[NSNull null] && _dataService.detailItem.replyStatus.integerValue == 1) {
            cell.detailTextView.text = @"已回复";
        } else {
            cell.detailTextView.text = @"未回复";
        }
    } else if (indexPath.row == 2) {
        cell.titleLabel.text = @"受理部门";
        cell.detailTextView.text = _dataService.detailItem.unitName;
    } else if (indexPath.row == 3) {
        cell.titleLabel.text = @"回复时间";
        if (_dataService.detailItem.replyTime && _dataService.detailItem.replyTime != (id)[NSNull null] && _dataService.detailItem.replyTime.length > 0) {
            cell.detailTextView.text = _dataService.detailItem.replyTime;
        } else {
            cell.detailTextView.text = @"";
        }
    } else if (indexPath.row == 4) {
        cell.titleLabel.text = @"回复内容";
        if (_dataService.detailItem.replyContent && _dataService.detailItem.replyContent != (id)[NSNull null] && _dataService.detailItem.replyContent.length > 0) {
            cell.detailTextView.text = _dataService.detailItem.replyContent;
        } else {
            cell.detailTextView.text = @"";
        }
    }
//    cell.detailTextView.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
